<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资金账号" prop="atAcc">
        <el-input v-model="queryParams.atAcc" placeholder="请输入资金账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="交易账号" prop="tdAcc">
        <el-input v-model="queryParams.tdAcc" placeholder="请输入交易账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="买入手续费" prop="buyRate">
        <el-input
          v-model="queryParams.buyRate"
          placeholder="请输入买入手续费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卖出手续费" prop="sellRate">
        <el-input
          v-model="queryParams.sellRate"
          placeholder="请输入卖出手续费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="冻结资金" prop="djCash">
        <el-input
          v-model="queryParams.djCash"
          placeholder="请输入冻结资金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可用资金" prop="kyCash">
        <el-input
          v-model="queryParams.kyCash"
          placeholder="请输入可用资金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易密码" prop="tdPwd">
        <el-input
          v-model="queryParams.tdPwd"
          placeholder="请输入交易密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可用资金" prop="cash">
        <el-input
          v-model="queryParams.cash"
          placeholder="请输入可用资金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用" prop="cost">
        <el-input
          v-model="queryParams.cost"
          placeholder="请输入费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="锁定费用" prop="aaLock">
        <el-input
          v-model="queryParams.aaLock"
          placeholder="请输入锁定费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:tradeAccount:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:tradeAccount:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:tradeAccount:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:tradeAccount:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tradeAccountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />

      <el-table-column label="资金账号" align="center" prop="atAcc" />
      <el-table-column label="交易账号" align="center" prop="tdAcc" />
      <el-table-column label="账号类型" align="center" prop="tdType"><template slot-scope="scope">
          <dict-tag :options="dict.type.account_type" :value="scope.row.tdType" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.account_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="是否免五" align="center" prop="ffive">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_five" :value="scope.row.ffive" />
        </template>
      </el-table-column>
      <el-table-column label="买入手续费" align="center" prop="buyRate" />
      <el-table-column label="卖出手续费" align="center" prop="sellRate" />
      <el-table-column label="冻结资金" align="center" prop="djCash" />
      <el-table-column label="可取资金" align="center" prop="kqZj" />
      <!-- <el-table-column label="交易密码" align="center" prop="tdPwd" /> -->
      <el-table-column label="可用资金" align="center" prop="cash" />
      <!-- <el-table-column label="费用" align="center" prop="cost" /> -->
      <el-table-column label="锁定费用" align="center" prop="aaLock" />
      <el-table-column label="融资比例" align="center" prop="financingRatio" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleOrder(scope.row)"
            v-hasPermi="['business:tradeAccount:edit']">成交订单</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="goToUserPage(scope.row)">充值</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:tradeAccount:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:tradeAccount:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改交易账号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="资金账号" prop="atAcc">
              <el-input v-model="form.atAcc" placeholder="请输入资金账号" />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="交易账号" prop="tdAcc">
              <el-input v-model="form.tdAcc" placeholder="请输入交易账号" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label-width="90px" label="买入手续费" prop="buyRate">
              <el-input v-model="form.buyRate" placeholder="请输入买入手续费" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="90px" label="卖出手续费" prop="sellRate">
              <el-input v-model="form.sellRate" placeholder="请输入卖出手续费" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="冻结资金" prop="djCash">
              <el-input v-model="form.djCash" placeholder="请输入冻结资金" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可取资金" prop="kqZj">
              <el-input v-model="form.kqZj" placeholder="请输入可取资金" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码" prop="tdPwd">
              <el-input v-model="form.tdPwd" placeholder="请输入登录密码，不改默认为原值！" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可用资金" prop="cash">
              <el-input v-model="form.cash" placeholder="请输入可用资金" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否免五" prop="ffive">
              <el-select v-model="form.ffive" placeholder="请输入是否免五" clearable>
                <el-option v-for="dict in dict.type.sys_is_five" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号类型" prop="tdType">
              <el-select v-model="form.tdType" placeholder="请输入账号类型" clearable>
                <el-option v-for="dict in dict.type.account_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请输入账号状态类型" clearable>
                <el-option v-for="dict in dict.type.account_status" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="融资比例" prop="financingRatio">
              <el-input v-model="form.financingRatio" placeholder="请输入融资比例" @input="handlePercentInput(form.financingRatio)" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="费用" prop="cost">
              <el-input v-model="form.cost" placeholder="请输入费用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="锁定费用" prop="aaLock">
              <el-input v-model="form.aaLock" placeholder="请输入锁定费用" />
            </el-form-item>
          </el-col> -->
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="open2" width="1000px" append-to-body>
      <el-table v-loading="loading" :data="mlTradeEntrustOrderList" @selection-change="handleSelectionChange">
        <el-table-column label="交易账号" align="center" prop="tdAcc" />
        <el-table-column label="标的代码" align="center" prop="symbol" />
        <el-table-column label="股票名称" align="center" prop="secName" />
        <el-table-column label="委托单号" align="center" prop="wtDh" />
        <el-table-column label="委托方向" align="center" prop="wtFx">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_wt_fx" :value="scope.row.wtFx" />
          </template>
        </el-table-column>
        <el-table-column label="委托价格" align="center" prop="wtJg">
          <template slot-scope="scope">
            <el-input v-model="scope.row.wtJg" @change="handleChange(scope.$index, scope.row)"></el-input>
          </template>
        </el-table-column>

        <el-table-column label="委托数量" align="center" prop="wtSl">
          <template slot-scope="scope">
            <el-input v-model="scope.row.wtSl" @change="handleChange(scope.$index, scope.row)"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="orderCompletion(scope.row)">确定成交</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
    </el-dialog>
    <el-dialog :title="title" :visible.sync="open3" width="700px" append-to-body>
      <el-form ref="form3" :model="form3" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="金额" prop="cash">
              <el-input v-model="form3.cash" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易账号" label-width="90px" prop="tdAcc">
              <el-input v-model="form3.tdAcc" placeholder="请输入交易账号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="crLx">
              <el-select v-model="form3.crLx" placeholder="请输入类型" clearable :disabled="true">
                <el-option v-for="dict in dict.type.sys_cr_lx" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="crZt">
              <el-select v-model="form3.crZt" placeholder="请输入状态" clearable :disabled="true">
                <el-option v-for="dict in dict.type.sys_cr_zt" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form3.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm3">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTradeAccount, getTradeAccount, delTradeAccount, getOrder, addTradeAccount, updateTradeAccount, entrustOrder } from "@/api/business/tradeAccount";
import { addApply } from "@/api/business/apply";

export default {
  name: "TradeAccount",
  dicts: ['account_status', 'account_type', 'sys_is_five', 'sys_wt_fx','sys_cr_zt', 'sys_cr_lx'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 交易账号表格数据
      tradeAccountList: [],
      mlTradeEntrustOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      open3: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        atAcc: null,
        tdAcc: null,
        tdType: null,
        status: null,
        ffive: null,
        buyRate: null,
        sellRate: null,
        djCash: null,
        kyCash: null,
        tdPwd: null,
        cash: null,
        cost: null,
        aaLock: null,

      },
      // 表单参数
      form: {},
      form3:{
        cash:0,
        crLx:0,
        crZt:1
      },
      // 表单校验
      rules: {
        atAcc: [
          { required: true, message: "资金账号不能为空", trigger: "blur" }
        ],
        tdAcc: [
          { required: true, message: "交易账号不能为空", trigger: "blur" }
        ],
        tdType: [
          { required: true, message: "账号类型不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        ffive: [
          { required: true, message: "是否免五不能为空", trigger: "change" }
        ],
        financingRatio: [
          { required: true, message: "融资比例不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitForm3() {
      addApply(this.form3).then(response => {
        this.$modal.msgSuccess("充值成功");
        this.open3 = false;
        this.getList();
      });
    },
    /** 充值 */
    goToUserPage(row) {
      const id = row.id || this.ids
      getTradeAccount(id).then(response => {
        this.form3 = response.data;
        this.form3.crLx=0
        this.form3.cash=0
        this.form3.crZt=1
        this.open3 = true;
        this.title = "账户充值";
      });
    },
    handlePercentInput(val) {
      debugger
      if (!val.endsWith('%')) {
        this.form.financingRatio = val.replace('%', '') + '%';
      }
    },
    /** 查询交易账号列表 */
    getList() {
      this.loading = true;
      listTradeAccount(this.queryParams).then(response => {
        this.tradeAccountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        updateTime: null,
        remarks: null,
        atAcc: null,
        tdAcc: null,
        tdType: null,
        status: null,
        ffive: null,
        buyRate: null,
        sellRate: null,
        djCash: null,
        kyCash: null,
        cash: null,
        cost: null,
        aaLock: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加交易账号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTradeAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改交易账号";
      });
    },
    handleOrder(row) {
      this.reset();
      const tdAcc = row.tdAcc
      getOrder(tdAcc).then(response => {
        this.mlTradeEntrustOrderList = response.data;
        this.open2 = true;
        this.title = "委托订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTradeAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTradeAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    orderCompletion(row) {
      entrustOrder(row).then(response => {
        getOrder(row.tdAcc).then(response => {
          debugger
          this.mlTradeEntrustOrderList = response.data;
        });
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除交易账号编号为"' + ids + '"的数据项？').then(function () {
        return delTradeAccount(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/tradeAccount/export', {
        ...this.queryParams
      }, `tradeAccount_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
